package com.huangd.platform.common.aspect;


import com.huangd.platform.modules.sys.entity.TblUserEntity;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

@Aspect
@Component
public class AopLog {

    private static final Logger log = LoggerFactory.getLogger(AopLog.class);


    /**
     * 切入点
     */
    @Pointcut("execution(public * com..modules..*Controller.*(..))")
    public void log() {

    }

    /**
     * 前置操作
     *
     * @param point 切入点
     */
    @Before("log()")
    public void beforeLog(JoinPoint point) {

        TblUserEntity userEntity = (TblUserEntity) SecurityUtils.getSubject().getPrincipal();
        if (userEntity != null) {
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();

            log.info("后台日志,USER:{},name:{},ip:{},uri:{},param:{}", userEntity.getId(), userEntity.getName(), request.getRemoteAddr(),  request.getServletPath(), point.getArgs());
        }
    }
}